﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Wage_wages : ATPages
{
   
    ZSql zsql = new ZSql();
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
         if(!IsPostBack)
      {
        yjTm1.Attributes.Add("onclick", "SelectDate('yjTm1')");
        yjTm2.Attributes.Add("onclick", "SelectDate('yjTm2')");
        hfxs.Value = Request.QueryString["moduid"].ToString();
        lblwz.Text = f.getModuleidAdd(hfxs.Value);
        gvdata();
        initType1();
   
       }
       
    }
    private void gvdata()
    {
        string sql = "select a.userId,a.SalesId,b.StaffName,d.Deptname,b.StaffSalary ,a.comodName,a.modul,a.Unit,a.Prices,a.number,a.TotalMoney,(convert(char(10),C_date,120))as C_date,u.StaffId,d.Deptid from  CS_Staff as b  left join UserInfo as u on  b.StaffId=u.StaffId left join CS_Sales as a on a.Saleman=b.StaffId  left join Department as d on b.deptid=d.deptid order by d.Deptid desc ";
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;

    }

    protected void linkbtnSales_Click(object sender, EventArgs e)
    {

            string SalesId = (sender as LinkButton).CommandArgument.Split('|')[0].ToString();
            string modul = (sender as LinkButton).CommandArgument.Split('|')[1].ToString();
            string StaffName = (sender as LinkButton).CommandArgument.Split('|')[2].ToString();
            Response.Redirect("WageClearing.aspx?SalesId=" + SalesId + "&modul=" + modul + "&StaffName=" + StaffName + "&moduid=" + hfxs.Value + "");
     

    }
    protected void lnkbtnLook_Click(object sender, EventArgs e)
    {
        string StaffName = (sender as LinkButton).CommandArgument.Split('|')[0].ToString();
        string SalesId = (sender as LinkButton).CommandArgument.Split('|')[1].ToString();
        string Deptname = (sender as LinkButton).CommandArgument.Split('|')[2].ToString();
        Response.Redirect("ViewWages1.aspx?StaffName=" + StaffName + "&SalesId=" + SalesId + "&Deptname=" + Deptname + "&moduid=" + hfxs.Value + "");
   
    
    }
    protected void btnSelec_Click(object sender, EventArgs e)
    {
        if(yjTm1.Text=="" &&yjTm2.Text!="")
        {
            Response.Write("<script>alert('日期一不能为空');location.href=document.referrer</script>");
        }
        if(yjTm2.Text=="" &&yjTm1.Text!="")
        {
            Response.Write("<script>alert('日期二不能为空');location.href=document.referrer</script>");
            
        }
        
        ZSql selec = new ZSql();
        //int flag=0;
        //if (ddlzt.SelectedValue == "0"||ddlzt.SelectedValue=="2")
        //{
        //    flag = 0;
        //}
        //if (ddlzt.SelectedValue == "1")
        //{
        //    flag = 1;
        //}
        string sql = "select a.userId,a.SalesId,b.StaffName,d.Deptname,b.StaffSalary ,a.comodName,a.modul,a.Unit,a.Prices,a.number,a.TotalMoney,(convert(char(10),C_date,120))as C_date,u.StaffId,d.Deptid from  CS_Staff as b  left join UserInfo as u on  b.StaffId=u.StaffId left join CS_Sales as a on a.Saleman=b.StaffId  left join Department as d on b.deptid=d.deptid where 1=1";
        
        if (txtselecName.Text == "" && ddrDepartment.SelectedValue == "0" && yjTm1.Text == "" && yjTm2.Text == "")
        {
            sql += "" ;

        }
        else if (ddrDepartment.SelectedValue == "0" && yjTm1.Text == "" && yjTm2.Text == "")
        {
            sql += " and b.StaffName like'%" + Common.FormatParameter(txtselecName.Text) + "%'" ;

        }
        else if (txtselecName.Text == "" && yjTm1.Text == "" && yjTm2.Text == "")
        {
            sql += " and b.Deptid=" + ddrDepartment.SelectedValue + "";

        }
       else if(txtselecName.Text==""&&ddrDepartment.SelectedValue=="0")
        {
            sql += " and left(convert(char(10),C_date,120),7) >='" + Common.FormatParameter(f.CutStr(yjTm1.Text, 7)) + "' and left(convert(char(10),C_date,120),7) <='" + Common.FormatParameter(f.CutStr(yjTm2.Text, 7)) + "'";
        
        }
        else if (txtselecName.Text !="" && ddrDepartment.SelectedValue !="0" && yjTm1.Text==""&&yjTm2.Text=="")
        {
            sql += " and b.StaffName like'%" + Common.FormatParameter(txtselecName.Text) + "%' and b.Deptid=" + ddrDepartment.SelectedValue + " ";
        }
        else if(txtselecName.Text !="" && ddrDepartment.SelectedValue=="0" &&yjTm1.Text!=""&&yjTm2.Text!="")
        {
            sql += " and b.StaffName like'%" + Common.FormatParameter(txtselecName.Text) + "%' and left(convert(char(10),C_date,120),7) >='" + Common.FormatParameter(f.CutStr(yjTm1.Text, 7)) + "' and left(convert(char(10),C_date,120),7) <='" + Common.FormatParameter(f.CutStr(yjTm2.Text, 7)) + "'";
        }
        else if (txtselecName.Text == "" && ddrDepartment.SelectedValue != "0" && yjTm1.Text != "" && yjTm2.Text != "")
        {
            sql += " and b.Deptid=" + ddrDepartment.SelectedValue + " and left(convert(char(10),C_date,120),7) >='" + Common.FormatParameter(f.CutStr(yjTm1.Text, 7)) + "' and left(convert(char(10),C_date,120),7) <='" +Common.FormatParameter(f.CutStr(yjTm2.Text, 7)) + "'";
        }
        else
        {
            sql += " and b.StaffName like'%" + Common.FormatParameter(txtselecName.Text) + "%' and b.Deptid=" + ddrDepartment.SelectedValue + " and left(convert(char(10),C_date,120),7) >='" + Common.FormatParameter(f.CutStr(yjTm1.Text, 7)) + "' and left(convert(char(10),C_date,120),7) <='" + Common.FormatParameter(f.CutStr(yjTm2.Text, 7)) + "'";
        }

        sql += " order by d.Deptid desc";
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
      
    }
    private void initType1()
    {
        ZSql Dep = new ZSql();
        Dep.Open("select * from Department");
        ddrDepartment.DataSource = Dep.m_table;
        ddrDepartment.DataTextField = "Deptname";
        ddrDepartment.DataValueField = "Deptid";
        ddrDepartment.DataBind();
        ddrDepartment.Items.Insert(0, new ListItem("—选择所属部门—", "0"));
        Dep.Close();
    }



    protected void GvDataJs_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        f.checkLevels(Request.Cookies["user"].Values["roleid"], Request["moduid"], 10, GvDataJs);
        pdkz();
        jszt();
        
    }
    private void jszt()
    {
        
        ZSql jt = new ZSql();
        if (GvDataJs.Rows.Count > 0)
        {
            for (int i = 0; i < GvDataJs.Rows.Count; i++)
            {
                string SalesId = GvDataJs.Rows[i].Cells[0].Text.ToString();
                string StaffName = GvDataJs.Rows[i].Cells[1].Text.ToString();
                string modul = GvDataJs.Rows[i].Cells[5].Text.ToString();
                string department = GvDataJs.Rows[i].Cells[2].Text.ToString();
                string Staffsalar = (GvDataJs.Rows[i].Cells[3].FindControl("StaffSalary") as Label).Text.ToString();
                string Staffperma = (GvDataJs.Rows[i].Cells[9].FindControl("salemenoy") as Label).Text.ToString();
                if (SalesId == "暂无业绩")
                {
                    jt.Open("select * from CS_Wages where WStaffName='" + StaffName + "' and department='" + department + "' order by WagesId desc");
                }
                else
                {
                    jt.Open("select * from CS_Wages where WStaffName='" + StaffName + "' and AuditState='" + SalesId + "' order by WagesId desc");
                }
                if (jt.m_table.Rows.Count > 0)
                {
                    if (jt.m_table.Rows[0]["department"].ToString() == department && jt.m_table.Rows[0]["WStaffSalary"].ToString().Substring(0, jt.m_table.Rows[0]["WStaffSalary"].ToString().Length - 2) == Staffsalar)
                    {
                        (GvDataJs.Rows[i].Cells[11].FindControl("linkbtnSales") as LinkButton).Enabled = false;
                        (GvDataJs.Rows[i].Cells[11].FindControl("linkbtnSales") as LinkButton).Text = "已结算";
                        //GvDataJs.Rows[i].BackColor = System.Drawing.Color.Yellow;
                        //GvDataJs.Rows[i].Attributes.Add("onmouseover", "{this.style.backgroundColor='#Efefef'}");
                        (GvDataJs.Rows[i].Cells[11].FindControl("lnkbtnLook") as LinkButton).Enabled = true;
                        if (ddlzt.SelectedValue == "0" || ddlzt.SelectedValue == "2")
                        {
                            GvDataJs.Rows[i].Visible = false;
                        }


                    }
                }
                else
                {
                    if (ddlzt.SelectedValue == "1")
                    {
                        GvDataJs.Rows[i].Visible = false;
                    }
                }
            }
        }
    }
    private void pdkz()
    {

        if (GvDataJs.Rows.Count > 0)
        {
            for (int i = 0; i < GvDataJs.Rows.Count; i++)
            {
                if (GvDataJs.Rows[i].Cells[0].Text == "&nbsp;")
                {
                    GvDataJs.Rows[i].Cells[0].Text = "暂无业绩";
                }
                if ((GvDataJs.Rows[i].Cells[9].FindControl("salemenoy") as Label).Text == "")
                {
                    (GvDataJs.Rows[i].Cells[9].FindControl("salemenoy") as Label).Text = "0.00";
                }

                if (GvDataJs.Rows[i].Cells[4].Text == "&nbsp;")
                {
                    GvDataJs.Rows[i].Cells[4].Text = "无";
                }
                if (GvDataJs.Rows[i].Cells[5].Text == "&nbsp;")
                {
                    GvDataJs.Rows[i].Cells[5].Text = "无";
                }
                if (GvDataJs.Rows[i].Cells[6].Text == "&nbsp;")
                {
                    GvDataJs.Rows[i].Cells[6].Text = "无";
                }
                if ((GvDataJs.Rows[i].Cells[7].FindControl("Prices") as Label).Text == "")
                {
                    (GvDataJs.Rows[i].Cells[7].FindControl("Prices") as Label).Text = "0.00";
                }
                if (GvDataJs.Rows[i].Cells[8].Text == "&nbsp;")
                {
                    GvDataJs.Rows[i].Cells[8].Text = "0";
                }
                if (GvDataJs.Rows[i].Cells[10].Text == "&nbsp;")
                {
                    GvDataJs.Rows[i].Cells[10].Text = "无";
                }
            }
        }

    }
}
